// page-shop/operation/operation.js
const app = getApp()
Page({
  data: {
    url: app.globalData.requestURL + '/upload/uploadFile2Qiniu',
    sex:['男','女'],
    indexSex:0,
    cardType: [
      '操作证',
      '评估师证',
      '维修技师证',
    ],
    indexCardType:0,
    deviceType: ['挖掘机', '装载机', '矿用自卸车', '推土机', '压路机', '平地机', '混凝土机械', '桩工机械', '凿岩机械', '摊铺机',  '俩头忙', '旋挖钻', '叉车'],
    indexDeviceType: 0,
    
    cardLevel: [
      '初级',
      '中级',
      '高级',
    ],
    indexCardLevel: 0,
    workYear: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30'],
    indexWorkYear: 0,
    imageList: [],//（个人身份证正反面，手持身份证和机器合照）
    countIndex: 3,//上传图片个人照数量
    userImgsUrl:'',
  },

  
  bindSexChange:function(e){
    this.setData({
      indexSex: e.detail.value
    })
  },
  bindCardTypeChange: function (e) {
    this.setData({
      indexCardType: e.detail.value
    })
  },
  bindCardLevelChange: function (e) {
    this.setData({
      indexCardLevel: e.detail.value
    })
  },
  bindPickerChangeDeviceType: function (e) {
    this.setData({
      indexDeviceType: e.detail.value
    })
  },

  bindWorkYearChange: function (e) {
    this.setData({
      indexWorkYear: e.detail.value
    })
  },
  chooseImageId: function () {
    var that = this
    if (this.data.imageList.length < this.data.countIndex) {
      wx.chooseImage({
        sourceType: ['camera'],
        sizeType: ['original', 'compressed'],
        count: this.data.countIndex,
        success: function (res) {
          console.log(res)
          that.setData({
            imageList: that.data.imageList.concat(res.tempFilePaths),
            name: "file",
            path: res.tempFilePaths,
            imagesType: "0"
          })
          app.uploadimg(that.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            that.setData({
               userImgsUrl: that.data.userImgsUrl + imgResponseUrl.data + ','
            })
            console.log("返回图片url" + imgResponseUrl.data)
          };


        }
      })
    } else {
      wx.showToast({
        title: '最多上传' + this.data.countIndex + '张图片',
        icon: 'loading',
        duration: 3000
      });
    }
  },
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  submit: function (e) {
    var name = e.detail.value.name
    var phone = e.detail.value.phone
    var address = e.detail.value.address
    var idCardNum = e.detail.value.identityNum
    var fromData = e.detail.value;
    fromData.remark = this.data.userImgsUrl;
    console.log(fromData)

    var nameVer = /^[\u4E00-\u9FA5]{1,8}$/; 
    var numNameSymVer = /^(?![0-9]+$)[\u4E00-\u9FA5-0-9]+$/;
    var phoneVer = /^[1][3,4,5,7,8][0-9]{9}$/;
    var idCaedVer = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;


    if (!nameVer.test(name)) {
      wx.showToast({
        title: '姓名不正确',
        icon: 'loading'
      })
    } else if (!idCaedVer.test(idCardNum)) {
      wx.showToast({
        title: '身份证号不正确',
        icon: 'loading'
      })
    }else if (!phoneVer.test(phone)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'loading'
      })

    }
    else if (!numNameSymVer.test(address)) {
      wx.showToast({
        title: '地址不正确',
        icon: 'loading'
      })

    }
   
    else {
      wx.request({
        url: app.globalData.requestURL + '/handCertificateBasic/save',
        method: 'POST',
        data: fromData,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          wx.showToast({
            title: "提交成功"
          })
          setTimeout(function () {
            wx.navigateBack({
              url: '/pages/index/index'
            })
          }, 1500) //延迟时间 这里是1秒  
        },
        fail: function (res) {
          console.log('返回结果fail' + res.data.message)
        },

      })}

    
 
   
  },
  onLoad: function (options) {

  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },


  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },


  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})